//
//  NoticeTableViewCell.m
//  Contractor
//
//  Created by 金鑫 on 16/5/17.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "NoticeTableViewCell.h"
#import "noticCellBackview.h"


@implementation NoticeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        //标题
        self.title = [self creatlabel:14.0f color:[UIColor blackColor]] ;
        
        //大msg
        self.bigMsg = [self creatlabel:18.0f color:[UIColor blackColor]] ;
        
        //时间
        self.time = [self creatlabel:14.0f color:[UIColor grayColor]] ;
        
        //小msg
        self.smallMsg = [self creatlabel:14.0f color:[UIColor grayColor]] ;
        
        //背景图
        noticCellBackview *backView = [[noticCellBackview alloc] initWithFrame:CGRectMake(0, 0, __kScreenWidth, 70)] ;
        backView.backgroundColor = [UIColor whiteColor] ;
        [self.contentView addSubview:backView] ;
        self.backView = backView ;
    }
    return self ;
}

//创建label
- (UILabel *)creatlabel:(CGFloat) size color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init] ;
    label.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:size] ;
    label.backgroundColor = [UIColor clearColor] ;
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = color ;
    [self.contentView addSubview:label] ;
    return label ;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"cellID" ;
    NoticeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier] ;
    if (!cell) {
        cell = [[NoticeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    return cell ;
}

- (void)config:(NoticeModel *)model
{
    self.backgroundColor = [UIColor whiteColor] ;
    
    self.title.text = @"工单通知" ;
    self.bigMsg.text = model.msgtitle ;
    self.smallMsg.text = model.msg ;
    
    NSString *timeStamp2 = model.inputdatetime;
    long long int date1 = (long long int)[timeStamp2 intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSString *datestr = [NSString stringWithFormat:@"%@",date2] ;
    datestr = [datestr substringToIndex:19] ;
    NSLog(@"时间戳转日期 %@  = %@", timeStamp2, datestr);
    self.time.text = datestr ;
    
    [self setSubviewFrame] ;
    
}

- (void)setSubviewFrame
{
    CGSize titleLength = [self lengthwithLabel:self.title withsize:14.0f] ;
    self.title.frame = CGRectMake(10, 14, titleLength.width, 16.0f) ;
    
    self.bigMsg.frame = CGRectMake(10 + titleLength.width + 50, 12, __kScreenWidth - (10 + titleLength.width + 50), 22.0f) ;
    self.bigMsg.textAlignment = NSTextAlignmentLeft ;
    CGSize timeLength = [self lengthwithLabel:self.time withsize:14.0f] ;
    
    self.time.frame = CGRectMake(10, 40, timeLength.width, 16.0f) ;
    
    self.smallMsg.frame = CGRectMake(10 + timeLength.width + 10, 40, __kScreenWidth - (10 + timeLength.width + 10), 16.0f) ;
    self.smallMsg.textAlignment = NSTextAlignmentLeft ;
    
    [self.backView addSubview:self.title] ;
    [self.backView addSubview:self.bigMsg] ;
    [self.backView addSubview:self.time] ;
    [self.backView addSubview:self.smallMsg] ;
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
